using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Company.BLL.WS.KDT.GC;

namespace Company.BLL.KDT.GC
{
	public partial class PhuKienDangKy
	{
		#region Private members.

        protected long _ID;
        protected long _SoTiepNhan;
        protected long _ID_HopDong;
        protected int _trangthaixuly;
        protected string _So = string.Empty;
        protected string _Ma = string.Empty;
        protected DateTime _Ngay;
        protected string _NoiDung = string.Empty;
        protected string _GhiChu = string.Empty;
        protected string _ThongTinCu = string.Empty;
        protected string _ThongTinMoi = string.Empty;
        protected string _vbcp = string.Empty;
        protected string _maDaiLy = string.Empty;
        protected DateTime _NgayTiepNhan=new DateTime(1900,1,1);
        protected PhuKienCollection pkCollection=new PhuKienCollection();
		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
        public PhuKienCollection PKCollection
        {
            set { this.pkCollection = value; }
            get { return this.pkCollection; }
        }
		public long ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
		
		public long SoTiepNhan
		{
			set {this._SoTiepNhan = value;}
			get {return this._SoTiepNhan;}
		}
		
		public long HopDong_ID
		{
            set { this._ID_HopDong = value; }
            get { return this._ID_HopDong; }
		}
		
		public int TrangThaiXuLy
		{
			set {this._trangthaixuly = value;}
            get { return this._trangthaixuly; }
		}

        public string SoPhuKien
        {
            set { this._So = value; }
            get { return this._So; }
        }

        public string MaPhuKien
        {
            set { this._Ma = value; }
            get { return this._Ma; }
        }

        public DateTime NgayPhuKien
        {
            set { this._Ngay = value; }
            get { return this._Ngay; }
        }
        public DateTime NgayTiepNhan
        {
            set { this._NgayTiepNhan = value; }
            get { return this._NgayTiepNhan; }
        }
        public string NoiDung
        {
            set { this._NoiDung = value; }
            get { return this._NoiDung; }
        }

        public string GhiChu
        {
            set { this._GhiChu = value; }
            get { return this._GhiChu; }
        }

        public string ThongTinCu
        {
            set { this._ThongTinCu = value; }
            get { return this._ThongTinCu; }
        }

        public string ThongTinMoi
        {
            set { this._ThongTinMoi = value; }
            get { return this._ThongTinMoi; }
        }

        public string VBCP
        {
            set { this._vbcp = value; }
            get { return this._vbcp; }
        }

        public string MaDaiLy
        {
            set { this._maDaiLy = value; }
            get { return this._maDaiLy; }
        }
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
        public bool checkExitsSoPhuKien(string soPhuKien)
        {
            string spName = "select sophukien from  t_KDT_GC_PhuKienDangKy where sophukien=@sophukien";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(spName);
            db.AddInParameter(dbCommand, "@sophukien", SqlDbType.VarChar, soPhuKien);
            object o=db.ExecuteScalar(dbCommand);
            return (o != null);
        }
		

		public static PhuKienDangKy Load(long iD)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_Load";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, iD);
			PhuKienDangKy entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new PhuKienDangKy();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));                
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
			}
			reader.Close();
			dbCommand.Connection.Close();
			
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_HopDong_ID(long hopDong_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectBy_HopDong_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, hopDong_ID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		

		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderBy_HopDong_ID(long hopDong_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectBy_HopDong_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, hopDong_ID);
						
            return db.ExecuteReader(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		

		public static PhuKienDangKyCollection SelectCollectionBy_HopDong_ID(long hopDong_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectBy_HopDong_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, hopDong_ID);
						
            IDataReader reader = db.ExecuteReader(dbCommand);
			PhuKienDangKyCollection collection = new PhuKienDangKyCollection();			
			while (reader.Read())
			{
				PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                collection.Add(entity);
			}
			reader.Close();
			dbCommand.Connection.Close();
			return collection;			
			
		}
        public static PhuKienDangKyCollection SelectCollectionBy_HopDong_IDAndMaPhuKienDaDuyet(long hopDong_ID,string maphukien)
        {
            string sql = "Select * from t_KDT_GC_PhuKienDangKy where TrangThaiXuLy=1 and MaPhuKien='" + maphukien + "' and HopDong_ID=" + hopDong_ID;
             SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(sql);
            IDataReader reader = db.ExecuteReader(dbCommand);
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                collection.Add(entity);
            }
            reader.Close();            
            return collection;

        }
		//---------------------------------------------------------------------------------------------

        public static PhuKienDangKyCollection SelectCollectionBy_HopDong_IDANDSoPhuKienANDNgayPhuKien(long hopDong_ID,string sophukien,DateTime ngayphukien)
        {
            string spName = "p_KDT_GC_PhuKienDangKy_SelectBy_HopDong_IDAndSoPhuKienAndNgayPhuKien";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, hopDong_ID);
            db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar,sophukien);
            db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, ngayphukien);

            IDataReader reader = db.ExecuteReader(dbCommand);
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
              
                 collection.Add(entity);
            }
            reader.Close();
            dbCommand.Connection.Close();
            return collection;

        }

        public static PhuKienDangKyCollection SelectCollectionBy_HopDong_IDANDSoPhuKienANDNgayPhuKien(long hopDong_ID)
        {
            string spName = "p_KDT_GC_PhuKienDangKy_SelectBy_HopDong_IDOfPhuKien";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, hopDong_ID);
         
            IDataReader reader = db.ExecuteReader(dbCommand);
            PhuKienDangKyCollection collection = new PhuKienDangKyCollection();
            while (reader.Read())
            {
                PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                collection.Add(entity);
            }
            reader.Close();
            dbCommand.Connection.Close();
            return collection;

        }
		public static DataSet SelectAll()
        {
            string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static PhuKienDangKyCollection SelectCollectionAll()
		{
			PhuKienDangKyCollection collection = new PhuKienDangKyCollection();

			IDataReader reader = SelectReaderAll();
			while (reader.Read())
			{
				PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhuKien"))) entity.MaPhuKien = reader.GetString(reader.GetOrdinal("MaPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) entity.NoiDung = reader.GetString(reader.GetOrdinal("NoiDung"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) entity.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) entity.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy")); 
                collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_PhuKienDangKy_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static PhuKienDangKyCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			PhuKienDangKyCollection collection = new PhuKienDangKyCollection();

			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				PhuKienDangKy entity = new PhuKienDangKy();
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoPhuKien"))) entity.SoPhuKien = reader.GetString(reader.GetOrdinal("SoPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayPhuKien"))) entity.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("NgayPhuKien"));
                if (!reader.IsDBNull(reader.GetOrdinal("HopDong_ID"))) entity.HopDong_ID = reader.GetInt64(reader.GetOrdinal("HopDong_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
              	collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static long Insert(long soTiepNhan, long hopDong_ID, int trangThaiXuLy,string so,string ma,DateTime ngay,string noidung,string ghichu,string thongtincu,string thongtinmoi,string vbcp,string madaily,DateTime ngayTiepNhan)
		{
			PhuKienDangKy entity = new PhuKienDangKy();			
			entity.SoTiepNhan = soTiepNhan;
			entity.HopDong_ID = hopDong_ID;
			entity.TrangThaiXuLy = trangThaiXuLy;
            entity.SoPhuKien = so;
            entity.MaPhuKien = ma;
            entity.NgayPhuKien = ngay;
            entity.NoiDung = noidung;
            entity.GhiChu = ghichu;
            entity.ThongTinCu = thongtincu;
            entity.ThongTinMoi = thongtinmoi;
            entity.VBCP = vbcp;
            entity.MaDaiLy = madaily;
            entity.NgayTiepNhan = ngayTiepNhan;
			long returnID = entity.Insert();
						
			return returnID;
		}
		
		public bool Insert(PhuKienDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		
		public long Insert()
		{
			return this.InsertTransaction(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long InsertTransaction(SqlTransaction transaction)
		{			
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_Insert";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
			db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, this._ID_HopDong);
            db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._So);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.VarChar, this._Ma);
            db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._Ngay);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
            db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._vbcp);
            db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._maDaiLy);
            db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._trangthaixuly);

			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");				
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");				
			}
            foreach (PhuKien pk in PKCollection)
            {
                pk.Master_ID = this._ID;
                pk.InsertTransaction(transaction);
            }
            return this._ID;
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		public int InsertUpdate()
		{
			return this.InsertUpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdateTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_PhuKienDangKy_InsertUpdate";	
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
            db.AddOutParameter(dbCommand, "@IDOut", SqlDbType.BigInt, 8); 
            db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, this._ID_HopDong);
            db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._So);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.VarChar, this._Ma);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
            db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._Ngay);
            db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._vbcp);
            db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._maDaiLy);
            db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._trangthaixuly);
            int n = 0;
            if (transaction != null)
            {
               n= db.ExecuteNonQuery(dbCommand, transaction);                              
                                   
            }
            else
            {
                n=db.ExecuteNonQuery(dbCommand);
            }
            this._ID = (long)db.GetParameterValue(dbCommand, "@IDOut");				
            foreach (PhuKien pk in PKCollection)
            {
                pk.Master_ID = this._ID;
                pk.InsertUpdateTransaction(transaction);
            }
            return n;
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool InsertUpdate(PhuKienDangKyCollection collection)
        {
            bool ret;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.

        public static int Update(long soTiepNhan, long hopDong_ID, int trangThaiXuLy, string so, string ma, DateTime ngay, string noidung, string ghichu, string thongtincu, string thongtinmoi, string vbcp, string madaily,DateTime ngayTiepNhan)
		{
			PhuKienDangKy entity = new PhuKienDangKy();            
            entity.SoTiepNhan = soTiepNhan;
            entity.HopDong_ID = hopDong_ID;
            entity.TrangThaiXuLy = trangThaiXuLy;
            entity.SoPhuKien = so;
            entity.MaPhuKien = ma;
            entity.NgayPhuKien = ngay;
            entity.NoiDung = noidung;
            entity.GhiChu = ghichu;
            entity.ThongTinCu = thongtincu;
            entity.ThongTinMoi = thongtinmoi;
            entity.VBCP = vbcp;
            entity.NgayTiepNhan = ngayTiepNhan;
            entity.MaDaiLy = madaily;
			return entity.Update();
		}
		
		public int Update()
		{
			return this.UpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool Update(PhuKienDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.UpdateTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		public int UpdateTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_Update";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, this._ID_HopDong);
            db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, this._So);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.VarChar, this._Ma);
            db.AddInParameter(dbCommand, "@NgayPhuKien", SqlDbType.DateTime, this._Ngay);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
            db.AddInParameter(dbCommand, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            db.AddInParameter(dbCommand, "@ThongTinMoi", SqlDbType.NVarChar, this._ThongTinMoi);
            db.AddInParameter(dbCommand, "@VBCP", SqlDbType.NVarChar, this._vbcp);
            db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._maDaiLy);
            db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._trangthaixuly);
	
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int Delete(long iD)
		{
			PhuKienDangKy entity = new PhuKienDangKy();
			entity.ID = iD;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.DeleteTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool DeleteCollection(PhuKienDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKienDangKy item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		//---------------------------------------------------------------------------------------------

		public static void DeleteCollection(PhuKienDangKyCollection collection, SqlTransaction transaction)
		{			
            foreach (PhuKienDangKy item in collection)
            {
                item.DeleteTransaction(transaction);
            }			
		}

		public int DeleteTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_PhuKienDangKy_Delete";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		#endregion

        #region WebService

        public void WSSend()
        {

            PhuKienDangKyCollection pkdkCollection = PhuKienDangKy.SelectCollectionBy_HopDong_IDANDSoPhuKienANDNgayPhuKien(this.HopDong_ID, this.SoPhuKien, this.NgayPhuKien);
            int i = 0;
            HopDong hd = HopDong.Load(this._ID_HopDong);
            PhuKienDangKyInfo[] pkdkInfoCollection = new PhuKienDangKyInfo[pkdkCollection.Count];
            foreach (PhuKienDangKy pkdk in pkdkCollection)
            {
                PhuKienDangKyInfo pkdkInfo = new PhuKienDangKyInfo();
                pkdkInfo.GhiChu = pkdk.GhiChu;
                pkdkInfo.MaDoanhNghiep = hd.MaDoanhNghiep;
                pkdkInfo.MaHaiQuan = hd.MaHaiQuan;
                pkdkInfo.MaPhuKien = pkdk.MaPhuKien;
                pkdkInfo.NamTiepNhan = this.NgayTiepNhan.Year;
                pkdkInfo.NgayKy = hd.NgayKy;
                pkdkInfo.NgayPhuKien = pkdk.NgayPhuKien;
                pkdkInfo.NgayTiepNhan = pkdk.NgayTiepNhan;
                pkdkInfo.NoiDung = pkdk.NoiDung;
                pkdkInfo.SoHopDong = hd.SoHopDong;
                pkdkInfo.SoPhuKien = this.SoPhuKien;
                pkdkInfo.SoTiepNhan = this.SoTiepNhan;
                pkdkInfo.ThongTinCu = pkdk.ThongTinCu;
                pkdkInfo.ThongTinMoi = pkdk.ThongTinMoi;
                pkdkInfo.TrangThaiXL = pkdk.TrangThaiXuLy;
                pkdkInfo.VBCP = pkdk.VBCP;
                PhuKienCollection pkCollection = PhuKien.SelectCollectionBy_Master_ID(pkdk.ID);
                pkdkInfo.HangPhuKien = new PhuKienInfo[pkCollection.Count];
                int j = 0;
                foreach (PhuKien pk in pkCollection)
                {
                    PhuKienInfo pkInfo = new PhuKienInfo();
                    pkInfo.DonGia = pk.DonGia;
                    pkInfo.DVT_ID = pk.DVT_ID;
                    if (pk.NhomSP != null)
                        pkInfo.MaNhomSanPham = pk.NhomSP;
                    else
                        pkInfo.MaNhomSanPham = pk.MaHang;
                    pkInfo.MaDoanhNghiep = hd.MaDoanhNghiep;
                    pkInfo.MaHaiQuan = hd.MaHaiQuan;
                    pkInfo.MaHS = pk.MaHS;
                    pkInfo.MaPhuKien = pkdk.MaPhuKien;
                    pkInfo.NgayKy = hd.NgayKy;
                    pkInfo.NguyenTe_ID = pk.NguyenTe;
                    pkInfo.NuocXX_ID = pk.NuocXX_ID;
                    pkInfo.P_Code = pk.MaHang;
                    pkInfo.SoHopDong = hd.SoHopDong;
                    pkInfo.SoLuong = pk.SoLuong;
                    pkInfo.SoPhuKien = pkdk.SoPhuKien;
                    pkInfo.STTHang = pk.STTHang;
                    pkInfo.Ten = pk.TenHang;
                    pkInfo.ThongTinCu = pk.ThongTinCu;
                    pkInfo.TinhTrang = pk.TinhTrang;
                    pkInfo.TriGia = pk.TriGia;
                    pkdkInfo.HangPhuKien[j++] = pkInfo;
                }
                pkdkInfoCollection[i++] = pkdkInfo;

            }

            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            this.SoTiepNhan = kdt_gc.PhuKien_Send(pkdkInfoCollection);
            this.NgayTiepNhan = DateTime.Today;
            foreach (PhuKienDangKy pkdk in pkdkCollection)
            {
                pkdk.SoTiepNhan = this.SoTiepNhan;
                pkdk.NgayTiepNhan = DateTime.Today;
                pkdk.TrangThaiXuLy = 0;
            }
            InsertUpdate(pkdkCollection);
        }
        public bool WSCancel()
        {
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            PhuKienDangKyInfo pkdkInfo = new PhuKienDangKyInfo();
            pkdkInfo.SoTiepNhan = this.SoTiepNhan;
            pkdkInfo.MaHaiQuan = HopDong.Load(this.HopDong_ID).MaHaiQuan;
            pkdkInfo.NamTiepNhan = this.NgayTiepNhan.Year;
            bool kq = kdt_gc.PhuKien_Cancel(pkdkInfo);
            this.TrangThaiXuLy = -1;
            this.SoTiepNhan = 0;
            this.NgayTiepNhan = new DateTime(1900, 1, 1);
            PhuKienDangKyCollection pkdkCollection = PhuKienDangKy.SelectCollectionBy_HopDong_IDANDSoPhuKienANDNgayPhuKien(this.HopDong_ID, this.SoPhuKien, this.NgayPhuKien);
            foreach (PhuKienDangKy pkdk in pkdkCollection)
            {
                pkdk.SoTiepNhan = this.SoTiepNhan;
                pkdk.NgayTiepNhan = this.NgayTiepNhan;
                pkdk.TrangThaiXuLy = -1;
                pkdk.InsertUpdate();
            }
            //InsertUpdate(pkdkCollection);
            return kq;
        }

        public void WSRequest()
        {

            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            PhuKienDangKyInfo[] pkdkCollection = kdt_gc.PhuKien_Request(this.SoTiepNhan, HopDong.Load(this.HopDong_ID).MaHaiQuan, (short)this.NgayTiepNhan.Year);
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    PhuKienDangKyCollection pkdkCollectionClient = PhuKienDangKy.SelectCollectionBy_HopDong_IDANDSoPhuKienANDNgayPhuKien(this.HopDong_ID, this.SoPhuKien, this.NgayPhuKien);
                    PhuKienDangKy.DeleteCollection(pkdkCollectionClient, transaction);
                    pkdkCollectionClient.Clear();
                    foreach (PhuKienDangKyInfo entity in pkdkCollection)
                    {
                        PhuKienDangKy pkdk = new PhuKienDangKy();
                        pkdk.GhiChu = entity.GhiChu;
                        pkdk.HopDong_ID = this.HopDong_ID;
                        pkdk.MaPhuKien = entity.MaPhuKien;
                        pkdk.NgayPhuKien = entity.NgayPhuKien;
                        pkdk.NgayTiepNhan = this.NgayTiepNhan;
                        pkdk.NoiDung = entity.NoiDung;
                        pkdk.SoPhuKien = entity.SoPhuKien;
                        pkdk.SoTiepNhan = entity.SoTiepNhan;
                        pkdk.ThongTinCu = entity.ThongTinCu;
                        pkdk.ThongTinMoi = entity.ThongTinMoi;
                        pkdk.TrangThaiXuLy = entity.TrangThaiXL;
                        pkdk.VBCP = entity.VBCP;
                        pkdk.pkCollection = new PhuKienCollection();
                        //cap nhat thong tin phu kien vao gc
                        if (pkdk.TrangThaiXuLy == 1)
                        {
                            Company.BLL.GC.PhuKienDangKy pkdkDuyet = new Company.BLL.GC.PhuKienDangKy();
                            pkdkDuyet.GhiChu = entity.GhiChu;                            
                            pkdkDuyet.MaPhuKien = entity.MaPhuKien;
                            pkdkDuyet.NgayPhuKien = entity.NgayPhuKien;
                            pkdkDuyet.NgayTiepNhan = entity.NgayTiepNhan;
                            pkdkDuyet.NoiDung = entity.NoiDung;
                            pkdkDuyet.SoPhuKien = entity.SoPhuKien;                            
                            pkdkDuyet.ThongTinCu = entity.ThongTinCu;
                            pkdkDuyet.ThongTinMoi = entity.ThongTinMoi;
                            pkdkDuyet.SoTiepNhan = entity.SoTiepNhanHQDuyet;
                            pkdkDuyet.VBCP = entity.VBCP;
                            pkdkDuyet.MaDoanhNghiep = entity.MaDoanhNghiep;
                            pkdkDuyet.MahaiQuan = entity.MaHaiQuan;
                            pkdkDuyet.SoHopDong = entity.SoHopDong;
                            pkdkDuyet.NgayKy = entity.NgayKy;
                            pkdkDuyet.InsertTransaction(transaction);
                        }
                        if (entity.HangPhuKien.Length > 0)
                        {
                            foreach (PhuKienInfo info in entity.HangPhuKien)
                            {
                                PhuKien pk = new PhuKien();
                                pk.DonGia = info.DonGia;
                                pk.DVT_ID = info.DVT_ID;
                                pk.MaHang = info.P_Code;
                                pk.MaHS = info.MaHS;
                                pk.NguyenTe = info.NguyenTe_ID;
                                pk.NuocXX_ID = info.NuocXX_ID;
                                pk.SoLuong = info.SoLuong;
                                pk.STTHang = info.STTHang;
                                pk.TenHang = info.Ten;
                                pk.ThongTinCu = info.ThongTinCu;
                                pk.TinhTrang = info.TinhTrang;
                                pk.TriGia = info.TriGia;
                                pk.NhomSP = info.MaNhomSanPham;
                                pkdk.pkCollection.Add(pk);
                                //cap nhap hang phu kien vao csdl gc
                                if (entity.TrangThaiXL == 1)
                                {
                                    Company.BLL.GC.PhuKien pkDuyet = new Company.BLL.GC.PhuKien();
                                    pkDuyet.DonGia = info.DonGia;
                                    pkDuyet.DVT_ID = info.DVT_ID;                                    
                                    pkDuyet.MaDoanhNghiep = entity.MaDoanhNghiep;
                                    pkDuyet.MahaiQuan = entity.MaHaiQuan;
                                    pkDuyet.MaHang = info.P_Code;
                                    pkDuyet.MaHS = info.MaHS;
                                    pkDuyet.MaPhuKien = entity.MaPhuKien;
                                    pkDuyet.NgayKy = entity.NgayKy;                                    
                                    pkDuyet.NguyenTe_ID = info.NguyenTe_ID;
                                    pkDuyet.NhomSP = info.MaNhomSanPham;                                  
                                    pkDuyet.NuocXX_ID = info.NuocXX_ID;
                                    pkDuyet.SoHopDong = entity.SoHopDong;
                                    pkDuyet.SoLuong = info.SoLuong;
                                    pkDuyet.SoPhuKien = entity.SoPhuKien;
                                    pkDuyet.STTHang = info.STTHang;
                                    pkDuyet.TenHang = info.Ten;                                  
                                    pkDuyet.ThongTinCuHang = info.ThongTinCu;                                    
                                    pkDuyet.TinhTrang = info.TinhTrang;
                                    pkDuyet.TriGia = info.TriGia;                                    
                                    pkDuyet.InsertTransaction(transaction);
                                }
                            }
                        }
                        pkdk.InsertTransaction(transaction);                     
                    }
                    
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
            this.TrangThaiXuLy = pkdkCollection[0].TrangThaiXL;
        }
        
        #endregion WebService
	}	
}